"""
Case Type   : GUC
Case Name   : 修改参数vacuum_cost_page_dirty为其他数据类型，观察预期结果；
Create At   : 2020/11/19
Owner       : opentestcase023
Description :
         1、查询vacuum_cost_page_dirty默认值；
            show vacuum_cost_page_dirty;
         2、修改vacuum_cost_page_dirty为false，"test",9999999999等，并校验其预期结果；
            gs_guc set -D /openGauss/zyn1026_gauss/cluster/dn1 -c "vacuum_cost_page_dirty=9999999999"
Expect      :
        1、显示默认值；
        2、参数修改失败；
History     :
        modified by opentestcase013 at 2023-9-18:增加查看etc/hosts步骤，方便定位偶现停库失败问题
"""
import sys
import unittest
import subprocess

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH


class Deletaduit(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info('-----------Opengauss_Function_Guc_Resource_Case0066.py start------------')
        self.Constant = Constant()
        self.commonsh = CommonSH('dbuser')

    def test_startdb(self):
        # 查询该参数默认值;
        sql_cmd = self.commonsh.execut_db_sql(f'''show vacuum_cost_page_dirty;''')
        self.log.info(sql_cmd)
        self.res = sql_cmd.splitlines()[-2].strip()
        # 使用设置gs_guc set设置vacuum_cost_page_dirty为false
        msg = self.commonsh.execute_gsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'vacuum_cost_page_dirty=false')
        self.log.info(msg)
        self.assertFalse(msg)
        #修改失败后重启查看默认值
        status, etc_hosts = subprocess.getstatusoutput('cat /etc/hosts')
        self.log.info(etc_hosts)
        msg = self.commonsh.restart_db_cluster()
        status, etc_hosts = subprocess.getstatusoutput('cat /etc/hosts')
        self.log.info(etc_hosts)
        self.log.info(msg)
        self.assertTrue(msg)
        status = self.commonsh.get_db_cluster_status()
        self.assertTrue("Normal" in status)
        sql_cmd = self.commonsh.execut_db_sql(f'''show vacuum_cost_page_dirty;''')
        self.log.info(sql_cmd)
        self.assertIn(self.res, sql_cmd)
        # 使用设置gs_guc set设置vacuum_cost_page_dirty为test
        msg = self.commonsh.execute_gsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'vacuum_cost_page_dirty=test')
        self.log.info(msg)
        self.assertFalse(msg)
        # 修改失败后重启查看默认值
        status, etc_hosts = subprocess.getstatusoutput('cat /etc/hosts')
        self.log.info(etc_hosts)
        msg = self.commonsh.restart_db_cluster()
        status, etc_hosts = subprocess.getstatusoutput('cat /etc/hosts')
        self.log.info(etc_hosts)
        self.log.info(msg)
        self.assertTrue(msg)
        status = self.commonsh.get_db_cluster_status()
        self.assertTrue("Normal" in status)
        sql_cmd = self.commonsh.execut_db_sql(f'''show vacuum_cost_page_dirty;''')
        self.log.info(sql_cmd)
        self.assertIn(self.res, sql_cmd)
        # 使用设置gs_guc set设置vacuum_cost_page_dirty为9999999999
        msg = self.commonsh.execute_gsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'vacuum_cost_page_dirty=9999999999')
        self.log.info(msg)
        self.assertFalse(msg)
        # 修改失败后重启查看默认值
        status, etc_hosts = subprocess.getstatusoutput('cat /etc/hosts')
        self.log.info(etc_hosts)
        msg = self.commonsh.restart_db_cluster()
        status, etc_hosts = subprocess.getstatusoutput('cat /etc/hosts')
        self.log.info(etc_hosts)
        self.log.info(msg)
        self.assertTrue(msg)
        status = self.commonsh.get_db_cluster_status()
        self.assertTrue("Normal" in status)
        sql_cmd = self.commonsh.execut_db_sql(f'''show vacuum_cost_page_dirty;''')
        self.log.info(sql_cmd)
        self.assertIn(self.res, sql_cmd)

    def tearDown(self):
        self.log.info('----------------恢复默认值----------------')
        sql_cmd = self.commonsh.execut_db_sql(f'''show vacuum_cost_page_dirty;''')
        self.log.info(sql_cmd)
        if self.res not in sql_cmd:
            msg = self.commonsh.execute_gsguc('set', self.Constant.GSGUC_SUCCESS_MSG,
                                              f'''vacuum_cost_page_dirty={self.res}''')
            self.log.info(msg)
            status, etc_hosts = subprocess.getstatusoutput('cat /etc/hosts')
            self.log.info(etc_hosts)
            msg = self.commonsh.restart_db_cluster()
            status, etc_hosts = subprocess.getstatusoutput('cat /etc/hosts')
            self.log.info(etc_hosts)
            self.log.info(msg)
        status = self.commonsh.get_db_cluster_status()
        self.assertTrue("Normal" in status)
        self.log.info('--------------Opengauss_Function_Guc_Resource_Case0066.py执行完成---------------')
